package mskruch.gps.calculation;

import gpstools.Location;
import mskruch.gps.PointFactory;
import mskruch.gps.Position;

public class PositionsCalculatorImpl implements PositionsCalculator {
    /** most recent added point */
    private Position last = null;
    private double distance;

    PositionsCalculatorImpl() {
    }

    @Override
    public PositionsCalculator add(Position position) {
        if (last != null) {
            distance += position.distanceTo(last);
        }
        last = position;
        return this;
    }

    @Override
    public PositionsCalculator add(Location point) {
        add (PointFactory.newPosition(point.getLongitude(), point.getLatitude()));
        return this;
    }
    
    @Override
    public PositionsCalculator add(Position[] positions) {
        for (Position position : positions) {
            add(position);
        }
        return this;
    }

    @Override
    public double getDistance() {
        return distance;
    }

}
